package sparrowGui
{
	import com.greensock.TweenLite;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import sparrowGui.item.ComboboxItem;
	import sparrowGui.item.Item;
	import sparrowGui.utils.PubFun;
	import sparrowGui.vars.BaseComponentVars;

	/**
	 * 组合框
	 * vars里面的长宽滚动条参数均是list里面的参数
	 * comboItem里面的长宽为comboBox的长宽
	 * @author Pelephone
	 */	
	public class ComboBox extends Component
	{
		private var _list:VList;
		private var _mask:Sprite;
		private var _comboItem:Item;
		private var _tweenTime:Number;
		private var _upDown:int;
		
		private var tmparent:Sprite;
		
		/**
		 * @param data
		 * @param cbbSkin			下拉项皮肤
		 * @param LsItemSkinCla		list里面的item
		 * @param vars				list用的也是这个var
		 */
		public function ComboBox(data:Array,cbbSkin:Sprite=null,LsItemSkinCla:Class=null,vars:Object=null)
		{
			vars = vars || SparrowGUI.getIns().comboBoxVar;
			_list = new VList(data,LsItemSkinCla,vars);
			
			if(cbbSkin && cbbSkin.parent){
				var tmparent:DisplayObjectContainer = cbbSkin.parent;
				var id:int = cbbSkin.parent.getChildIndex(cbbSkin);
				vars.height = cbbSkin.height;
				vars.width = cbbSkin.width;
			}
			
			var itemdata:Object = (data && data.length)?data[0]:null;
			var cbVar:BaseComponentVars = (new BaseComponentVars());
			cbVar.isBgFill = vars.isBgFill;
			_comboItem = new ComboboxItem(itemdata,cbbSkin,cbVar);
			
			var cbbVars:Object = PubFun.parseObjToNew(vars,(new BaseComponentVars()));
			_tweenTime = vars.tweenTime;
			_upDown = vars.upDown;
			
			super(cbbVars);
			
			// 如果皮肤的父类不为空表示把场景里的对象显示对象换成此组合框，然后然皮肤放进此框里面
			if(tmparent){
				PubFun.copyProperties(this,cbbSkin,"x,y,name");
				tmparent.addChildAt(this,id);
				cbbSkin.x = cbbSkin.y = 0;
			}
		}
		
		override protected function init():void
		{
			addChild(_list);
			addChild(_comboItem);
			_comboItem.width = style.width;
			_mask = new Sprite();
			_list.visible = false;
			addChild(_mask);
			
			_list.addEventListener(RadioEvent.ITEM_SELECT,onListSelect);
			
			addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
			_comboItem.addEventListener(MouseEvent.CLICK,onMouseHandle);
		}
		
		// 设置选中第X项
		public function setSelectIndex(index:int):void
		{
			if((_list.itemsData as Array).indexOf(index)>=0)
				_comboItem.update(_list.itemsData[index]);
			
			_list.addSelectedIndex(index);
		}
		
		public function update(data:Array=null):void
		{
			var itemdata:Object = (data && data.length)?data[0]:null;
			_comboItem.update(itemdata);
			_list.update(data);
		}
		
		// 点击了 item弹出list 然后给舞台添加点击非列表就让列表消失
		private function onMouseHandle(e:MouseEvent):void
		{
//			onStageMouseDown(null);
			if(stage) stage.addEventListener(MouseEvent.MOUSE_UP,onStageMouseDown);
			_list.visible = true;
			
			addChild(_mask);
			_list.mask = _mask;
			var panHeight:int = _list.panHeight;
			var maskH:Number = (panHeight+10);
			_mask.graphics.clear();
			_mask.graphics.beginFill(0xFF0000);
			_mask.graphics.drawRect(0,0,(style.width+20),maskH);
			_mask.graphics.endFill();
			_mask.x = -10;
//			var maskLocalPt:Point = _comboItem.localToGlobal(new Point(_comboItem.x,_comboItem.y));
			var maskLocalPt:Point = _comboItem.localToGlobal(new Point(0,0));
			
			// 下部够地方显示或者上下都不够地方显示,就往下出列表框
			if(((maskLocalPt.y + panHeight+_comboItem.height)<stage.stageHeight && _upDown==0)
				|| ((maskLocalPt.y - panHeight)<0) || _upDown==1){
				_mask.y = _comboItem.height;
				_list.y = panHeight*(-1)+_comboItem.height;
				TweenLite.to(_list,Number(_tweenTime),{y:(_comboItem.height),onComplete:overMask});
				return;
			}
			
			_mask.y =_mask.height*(-1);
			_list.y = 0;
			TweenLite.to(_list,Number(_tweenTime),{y:(-panHeight),onComplete:overMask});
			
		}
		
		private function overMask():void
		{
			_list.mask = null;
			PubFun.SafeRemoveChild(_mask);
		}
		
		private function onListSelect(e:RadioEvent):void
		{
			_comboItem.update(_list.getFirstData());
			dispatchEvent(new RadioEvent(e.type,e.body));
		}
		
		// 点击了下拉之后给舞台增加点击事件,点任意地方都会使列表框消失.
		private function onStageMouseDown(e:MouseEvent):void
		{
			//判断是否点的是滚动条里的组件
			var deep:int = PubFun.isChild(_list.vScrollBar,(e.target as DisplayObject));
			if(!e || deep>=0) return;
//			if(!e || _list.isEventScrollBar(e)) return;

			if(stage){
				onRemovedFromStage();
				_list.visible = false;
			}
		}
		
		private function onRemovedFromStage(e:*=null):void
		{
			if(stage) stage.removeEventListener(MouseEvent.MOUSE_DOWN,onStageMouseDown);
		}
		
		override public function set width(w:Number):void
		{
			_comboItem.width = _list.width = w;
			super.width = w;
		}
		
		override public function set height(h:Number):void
		{
			_list.height = h;
		}

		public function get list():VList
		{
			return _list;
		}

		public function get comboItem():Item
		{
			return _comboItem;
		}
		
		override public function dispose():void
		{
			_list.removeEventListener(RadioEvent.ITEM_SELECT,onListSelect);
			onRemovedFromStage();
			
			_list.dispose();
			_comboItem.dispose();
			PubFun.SafeRemoveChild(_mask);
			_list = null;
			_comboItem = null;
			_mask = null;
			tmparent = null;
			
			super.dispose();
		}
	}
}